package com.androidhelper.module.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.androidhelper.module.common.app.Constants;
import com.androidhelper.module.common.app.LibApplication;

import androidx.annotation.NonNull;


public class SharePreferenceUtils
      //  implements SharedPreferenceHelper
{


    private    static SharePreferenceUtils sharePreferenceUtils;

    public static  SharePreferenceUtils getInstance(){
        if(sharePreferenceUtils==null){
            synchronized (SharePreferenceUtils.class){
              if(sharePreferenceUtils==null){
                  sharePreferenceUtils = new SharePreferenceUtils();
              }
            }

        }
        return  sharePreferenceUtils;
   }



    private final SharedPreferences mSharedPreferences;
    private SharePreferenceUtils(){
        mSharedPreferences= LibApplication.getInstance().getSharedPreferences(Constants.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
    }






    /**
     * Put the string value in sp.
     *
     * @param key   The key of sp.
     * @param value The value of sp.
     */

    public void putString(@NonNull final String key, final String value) {
        mSharedPreferences.edit().putString(key, value).apply();
    }

    /**
     * Return the string value in sp.
     *
     * @param key The key of sp.
     * @return the string value if sp exists or {@code ""} otherwise
     */

    public String getString(@NonNull final String key) {
        return mSharedPreferences.getString(key, "");
    }

    /**
     * Put the int value in sp.
     *
     * @param key   The key of sp.
     * @param value The value of sp.
     */
    public void putInt(@NonNull final String key, final int value) {
        mSharedPreferences.edit().putInt(key, value).apply();
    }
    /**
     * Return the int value in sp.
     *
     * @param key The key of sp.
     * @return the int value if sp exists or {@code -1} otherwise
     */
    public int getInt(@NonNull final String key) {
        return mSharedPreferences.getInt(key, -1);
    }


    /**
     * Put the long value in sp.
     *
     * @param key   The key of sp.
     * @param value The value of sp.
     */
    public void putLong(@NonNull final String key, final long value) {
        mSharedPreferences.edit().putLong(key, value).apply();
    }

    /**
     * Return the long value in sp.
     *
     * @param key The key of sp.
     * @return the long value if sp exists or {@code -1} otherwise
     */
    public long getLong(@NonNull final String key) {
        return mSharedPreferences.getLong(key, -1L);
    }



    /**
     * Put the float value in sp.
     *
     * @param key   The key of sp.
     * @param value The value of sp.
     */
    public void putFloat(@NonNull final String key, final float value) {
        mSharedPreferences.edit().putFloat(key, value).apply();
    }
    /**
     * Return the float value in sp.
     *
     * @param key The key of sp.
     * @return the float value if sp exists or {@code -1f} otherwise
     */
    public float getFloat(@NonNull final String key) {
        return mSharedPreferences.getFloat(key, -1f);
    }







    /**
     * Put the boolean value in sp.
     *
     * @param key   The key of sp.
     * @param value The value of sp.
     */
    public void putBoolean(@NonNull final String key, final boolean value) {
        mSharedPreferences.edit().putBoolean(key, value).apply();
    }

    /**
     * Return the boolean value in sp.
     *
     * @param key The key of sp.
     * @return the boolean value if sp exists or {@code false} otherwise
     */
    public boolean getBoolean(@NonNull final String key) {
        return mSharedPreferences.getBoolean(key, false);
    }




    /**
     * Remove the preference in sp.
     *
     * @param key The key of sp.
     */
    public void remove(@NonNull final String key) {
        mSharedPreferences.edit().remove(key).apply();
    }

    /**
     * Remove all preferences in sp.
     */
    public void clear() {
        mSharedPreferences.edit().clear().apply();
    }




   /* @Override
    public void setLoginStatus(boolean loginStatus) {
          putBoolean(Constants.SP_LOGIN_STATUS,loginStatus);
    }

    @Override
    public boolean getLoginStatus() {
        return getBoolean(Constants.SP_LOGIN_STATUS);
    }*/
}
